package com.europe.business.europebusiness.ui.activity;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.activity.base.ActionBarActivity;
import com.europe.business.europebusiness.ui.bean.JsonBean;
import com.europe.business.europebusiness.ui.bean.UserLogin;
import com.europe.business.europebusiness.ui.common.Commons;
import com.europe.business.europebusiness.ui.net.IUser;
import com.europe.business.europebusiness.ui.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisteredActivity extends ActionBarActivity implements View.OnClickListener {


    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    private int sort;

    private TextView mReg;
    private TextView getCode;
    private EditText userName;
    private EditText verCode;
    private EditText passw;


    @Override
    protected int setMainContent() {
        return R.layout.activity_registered;
    }

    @Override
    protected void initView() {
        super.initView();
        mReg = findViewById(R.id.regist);
        getCode = findViewById(R.id.get_ver);
        mReg.setOnClickListener(this);
        getCode.setOnClickListener(this);
        userName = findViewById(R.id.username_msg);

        verCode = findViewById(R.id.verify_code);
        passw = findViewById(R.id.password);
        setToolbarNavigationIcon(R.drawable.actionbar_back);
        setActionRight(false, "", null);

    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        sort = intent.getIntExtra("sort", 1);
        if (sort == SelectIdentityActivity.SORT_CHINA_PERSON) {
            userName.setHint(R.string.hint_username_phone);
            userName.setInputType(InputType.TYPE_CLASS_NUMBER);
            setTitle(getString(R.string.title_reg_person));
        } else {
            userName.setHint(R.string.hint_username_email);
            setTitle(getString(R.string.title_reg_company));
        }
    }

    @Override
    public void onClick(View v) {
        final String name = userName.getText().toString();
        final String pw = passw.getText().toString();
        Retrofit retrofit = null;
        switch (v.getId()) {
            case R.id.regist:
                Map<String, String> map = new HashMap<>();
                String code = verCode.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    showToastLong(getString(R.string.name_check));
                    return;
                } else {
                    if (sort == SelectIdentityActivity.SORT_CHINA_PERSON) {//手机号
                        if (!name.matches(REGEX_MOBILE)) {
                            showToastLong(getString(R.string.name_check_phone));
                            return;
                        }
                        map.put("mobile", name);
                    } else {
                        if (!name.matches(REGEX_EMAIL)) {
                            showToastLong(getString(R.string.name_check_email));
                            return;
                        }
                        map.put("email", name);
                    }
                }
                if (TextUtils.isEmpty(code)) {
                    showToastLong(getString(R.string.code_check));
                    return;
                }
                if (TextUtils.isEmpty(pw)) {
                    showToastLong(getString(R.string.password_check));
                    return;
                }
                map.put("password", pw);
                map.put("language", Utils.getSystemLanguage(this));
                map.put("verifyCode", code);
                map.put("type", sort + "");
                showProgressDialog();
                retrofit = new Retrofit.Builder()
                        .addConverterFactory(GsonConverterFactory.create())
                        .baseUrl(Commons.SERVER_URL)
                        .build();
                IUser user = retrofit.create(IUser.class);
//                Call<UserLogin> call = user.register(name, code, pw, sort, Utils.getSystemLanguage(this));
                Call<UserLogin> call = user.register(map);
                call.enqueue(new Callback<UserLogin>() {
                    @Override
                    public void onResponse(Call<UserLogin> call, Response<UserLogin> response) {
                        dismissProgressDialog();
                        if (null != response) {
                            UserLogin bean = response.body();
                            if (bean.getErrorCode() == 1) {
                                Intent intent = new Intent(RegisteredActivity.this, InfoCompanyActivity.class);
                                if (sort == SelectIdentityActivity.SORT_CHINA_PERSON) {
                                    intent.putExtra("accountId", bean.getData().getId());
                                    intent.putExtra("username", name);
                                    intent.putExtra("password", pw);
                                    startAct(intent);
                                } else {
                                    intent.putExtra("accountId", bean.getData().getId());
                                    intent.putExtra("sort", sort);
                                    intent.putExtra("username", name);
                                    intent.putExtra("password", pw);
                                    startAct(intent);
                                }

                                showToastLong(bean.getErrorMessage());
                            } else {
                                showToastLong(bean.getErrorMessage());
                            }
                        } else {
                            showToastLong(getString(R.string.reg_failure));
                        }
                    }

                    @Override
                    public void onFailure(Call<UserLogin> call, Throwable t) {
                        dismissProgressDialog();
                        Log.e("Login_err", "失败" + t.getMessage());
                        showToastLong(getString(R.string.reg_failure));
                    }
                });
                break;

            case R.id.get_ver:

                if (TextUtils.isEmpty(name)) {
                    showToastLong(getString(R.string.name_check));
                    return;
                } else {
                    if (sort == SelectIdentityActivity.SORT_CHINA_PERSON) {//手机号
                        if (!name.matches(REGEX_MOBILE)) {
                            showToastLong(getString(R.string.name_check_phone));
                            return;
                        }
                    } else {
                        if (!name.matches(REGEX_EMAIL)) {
                            showToastLong(getString(R.string.name_check_email));
                            return;
                        }
                    }
                }

                showProgressDialog();
                retrofit = new Retrofit.Builder()
                        .addConverterFactory(GsonConverterFactory.create())
                        .baseUrl(Commons.SERVER_URL)
                        .build();
                IUser user1 = retrofit.create(IUser.class);
                Call<JsonBean> call1 = user1.getCode(name, Utils.getSystemLanguage(this));
                call1.enqueue(new Callback<JsonBean>() {
                    @Override
                    public void onResponse(Call<JsonBean> call, Response<JsonBean> response) {
                        dismissProgressDialog();
                        if (null != response) {
                            JsonBean jsonBean = response.body();
                            if (jsonBean != null) {
                                if (jsonBean.getErrorCode() == 1) {
                                    verCode.setText(jsonBean.getData());
                                    showToastLong(jsonBean.getErrorMessage());
                                } else {
                                    showToastLong(jsonBean.getErrorMessage());
                                }
                            } else {
                                showToastLong(getString(R.string.requst_failed));
                            }

                            Log.i("Reg", response.body() + "");
                        } else {
                            showToastLong(getString(R.string.getcode_failure));
                        }
                    }

                    @Override
                    public void onFailure(Call<JsonBean> call, Throwable t) {
                        dismissProgressDialog();
                        Log.i("Login_err", "失败" + t.getMessage());
                        showToastLong(getString(R.string.getcode_failure));
                    }
                });
                break;
        }
    }
}
